import {useAccountStore} from "./AccountStore.ts";
import {defineStore} from "pinia";
import {CacheMap, useCache} from "../utils/CacheUtils.ts";
import type {ItemDataBody, ItemTypeInfo} from "../network/types/Item.ts";

const api = () => useAccountStore().getNgaApi().item

export const useItemStore = defineStore('ItemStore', {
    state: () => ({
        cache: new CacheMap<ItemTypeInfo[]>(),
        cacheItemList: new CacheMap<ItemDataBody>(),
    }),
    actions: {
        info(types: number[], force?: boolean) {
            const key = `info ${types}`
            return useCache(this.cache, key, force || false, () => api().info(types))
        },
        infoByType(type: number, subType: number[], force?: boolean) {
            const key = `info by type ${subType} ${type}`
            return useCache(this.cache, key, force || false, () => api().infoByType(type, subType))
        },
        store(page?: number | undefined, type?: number | undefined, subType?: number[] | undefined, force?: boolean) {
            const key = `store ${page} ${type} ${subType}`
            return useCache(this.cacheItemList, key, force || false, () => api().store(page, type, subType))
        },
        inventory(page?: number | undefined, force?: boolean) {
            const key = `inventory ${page}`
            return useCache(this.cacheItemList, key, force || false, () => api().inventory(page))
        },
        use(id: number, act: "use" | "buy_and_use", arg: number | number[]) {
            return api().use(id, act, arg)
        },
        buy(id: number) {
            return api().buy(id)
        },
    },
})